package jm.easyconfig.standalone;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import jm.easyconfig.CfgAdminServlet;
import jm.easyconfig.CfgContextEnvironmentAware;

@SpringBootApplication
public class EasyconfigStandaloneApplication {

	public static void main(String[] args) {
		SpringApplication.run(EasyconfigStandaloneApplication.class, args);
	}

	@Bean
	@ConditionalOnMissingBean(CfgContextEnvironmentAware.class)
	CfgContextEnvironmentAware cfgContextEnvironmentAware() {
		return new CfgContextEnvironmentAware();
	}

	@Bean
	public ServletRegistrationBean<CfgAdminServlet> cfgAdminServlet() {
		ServletRegistrationBean<CfgAdminServlet> bean = new ServletRegistrationBean<CfgAdminServlet>(
				new CfgAdminServlet(), "/resources/*");
		bean.setName("easyconfig-admin-servlet");
		return bean;
	}

	@Bean

	public PropertiesFactoryBean propertiesFactoryBean() {
		PropertiesFactoryBean pfb = new PropertiesFactoryBean();
		final PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
		
		pfb.setIgnoreResourceNotFound(true);
		final List<Resource> resourceLst = new ArrayList<Resource>();
		resourceLst.add(new ClassPathResource("my2.properties"));
		pfb.setLocations(resourceLst.toArray(new Resource[] {}));
		
		return pfb;
	}

	@Bean
	public PropertySourcesPlaceholderConfigurer properties2() {
		final PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
		ppc.setIgnoreResourceNotFound(true);
		final List<Resource> resourceLst = new ArrayList<Resource>();
		resourceLst.add(new ClassPathResource("my2.properties"));
		ppc.setLocations(resourceLst.toArray(new Resource[] {}));
		return ppc;
	}

//	@Bean
//	public PropertyPlaceholderConfigurer properties() {
//		final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
//		ppc.setIgnoreResourceNotFound(true);
//		final List<Resource> resourceLst = new ArrayList<Resource>();
//		resourceLst.add(new ClassPathResource("my.properties"));
//		ppc.setLocations(resourceLst.toArray(new Resource[] {}));
//		return ppc;
//	}

//	@Bean
//	@ConditionalOnMissingBean(CfgServletRegistrationBean.class)
//	public CfgServletRegistrationBean cfgAdminServlet() {
//		return new CfgServletRegistrationBean("/resources/*");
//	}

//	@Autowired
//	HttpSession session;
//
//	@Bean
//	public CfgContextUser currentUser() {
//		return new CfgContextUser() {
//			@Override
//			public String getUser() {
//				Object user = session.getAttribute("user");
//				return user == null ? "" : user.toString();
//			}
//		};
//	}

}
